﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Book
{
    public partial class UpdateInfo : Form
    {
        public UpdateInfo()
        {
            InitializeComponent();
        }
        //构造函数进行传值(点击修改之后把原先的信息显示在框内)
        private int id;
        public UpdateInfo(int id,string title, string author, DateTime time)
        {
            InitializeComponent();
            this.id = id;
            txt_Title.Text = title;
            txt_Author.Text = author;
            txt_Time.Text = time.ToString();
        }

        //SqlConnection sqlcon = new SqlConnection(DbHelper.constr);
        /// <summary>
        /// 确认修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            var title = txt_Title.Text;
            var author = txt_Author.Text;
            var time = txt_Time.Text;
            var nullTitle = string.IsNullOrEmpty(title);
            var nullAuthor = string.IsNullOrEmpty(author);
            if (!nullTitle && !nullAuthor)
            {
                string sql = string.Format("update Articles set Title='{0}',Author='{1}',CreatedTime='{2}' where Id={3}", title, author, time, id);
                var res = DbHelper.GetAddOrDelOrUp(sql);
                if (res >=1)
                {
                    MessageBox.Show("修改成功！", "恭喜");
                    BookInfo book = new BookInfo();
                    book.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败！", "错误");
                }
            }
            else
            {
                if (nullTitle && nullAuthor)
                {
                    MessageBox.Show("书名和作者不能为空！", "消息提示");
                }
                else if (nullTitle)
                {
                    MessageBox.Show("书名不能为空！", "消息提示");
                }
                else if (nullAuthor)
                {
                    MessageBox.Show("作者不能为空！", "消息提示");
                }
                else
                {
                    MessageBox.Show("错误提示", "错误");
                }
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
            BookInfo book = new BookInfo();
            book.Show();
        }
    }
}
